<html>
<head><meta charset="utf-8"><title>Should `Box` really behave like `&amp;mut`? · t-lang/wg-unsafe-code-guidelines · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/index.html">t-lang/wg-unsafe-code-guidelines</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Should.20.60Box.60.20really.20behave.20like.20.60.26mut.60.3F.html">Should `Box` really behave like `&amp;mut`?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="166652253"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Should%20%60Box%60%20really%20behave%20like%20%60%26mut%60%3F/near/166652253" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Should.20.60Box.60.20really.20behave.20like.20.60.26mut.60.3F.html#166652253">(May 27 2019 at 17:14)</a>:</h4>
<p><code>Box</code> asserting uniqueness like <code>&amp;mut</code> does has some very annoying consequences:</p>
<div class="codehilite"><pre><span></span><span class="w">    </span><span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">into_vec</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="p">(</span><span class="k">mut</span><span class="w"> </span><span class="n">b</span>: <span class="nb">Box</span><span class="o">&lt;</span><span class="p">[</span><span class="n">T</span><span class="p">]</span><span class="o">&gt;</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nb">Vec</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="kd">let</span><span class="w"> </span><span class="n">xs</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">Vec</span>::<span class="n">from_raw_parts</span><span class="p">(</span><span class="n">b</span><span class="p">.</span><span class="n">as_mut_ptr</span><span class="p">(),</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">len</span><span class="p">(),</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">len</span><span class="p">());</span><span class="w"></span>
<span class="w">            </span><span class="n">mem</span>::<span class="n">forget</span><span class="p">(</span><span class="n">b</span><span class="p">);</span><span class="w"></span>
<span class="w">            </span><span class="n">xs</span><span class="w"></span>
<span class="w">        </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
</pre></div>


<p>This is now incorrect because <code>mem::forget(b)</code> is a "reborrow"-like use of <code>b</code> and thus asserts uniqueness, breaking the raw pointer that was created just before with <code>as_mut_ptr</code> :/</p>



<a name="166652295"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Should%20%60Box%60%20really%20behave%20like%20%60%26mut%60%3F/near/166652295" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Should.20.60Box.60.20really.20behave.20like.20.60.26mut.60.3F.html#166652295">(May 27 2019 at 17:15)</a>:</h4>
<p>(and not doing the "reborrow" wouldn't help, we mark <code>Box</code> as <code>noalias</code> and as such we promise to <code>mem::forget</code> that this is a unique pointer...</p>



<a name="166652758"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Should%20%60Box%60%20really%20behave%20like%20%60%26mut%60%3F/near/166652758" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Should.20.60Box.60.20really.20behave.20like.20.60.26mut.60.3F.html#166652758">(May 27 2019 at 17:24)</a>:</h4>
<p>(the fix is to use <code>Box::into_raw</code>)</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>